﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
using QuanLyPhongMach;
using QuanLyPhongMach.Context;
using QuanLyPhongMach.Model;
using QuanLyPhongMach.Platform;
using System.Data;
using System.Collections;

namespace QuanLyPhongMach.View
{
    /// <summary>
    /// Interaction logic for WKhamBenh.xaml
    /// </summary>
    public partial class WKhamBenh : Page
    {
        #region VARIABLE

        private CThuocContext thuoc;
        private CDonViContext donvi;
        private CBenhContext benh;
        private CCachDungContext cachdung;
        private CKhamBenhContext khambenh;
        private CChiTietKhamBenhContext chitietkhambenh;
        private CBenhNhanContext benhnhan;
        private CThamSoContext thamso;
        private List<CChiTietKhamBenh> listChiTiet;

        private int intSTT;
        private int makhambenh, tienkham;
        private int mathuoc, madonvi, macachdung, soluong, dongiaban, tienthuoc;
        #endregion

        public WKhamBenh()
        {
            InitializeComponent();
            TaoBang();
            ThemChiTiet();
            listChiTiet = new List<CChiTietKhamBenh>();
        }

        #region EVENT

        private void btnPhieuMoi_Click(object sender, RoutedEventArgs e)
        {
            LamMoi();
        }

        private void btnLapPhieu_Click(object sender, RoutedEventArgs e)
        {
            LapPhieu();
        }

        private void txtMaBenhNhan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtTrieuChung.Focus();
            }
        }

        private void txtMaBenhNhan_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                txtTenBenhNhan.Text = benhnhan.LayThongTin(int.Parse(txtMaBenhNhan.Text)).HoTen;
            }
            catch
            {
                txtTenBenhNhan.Text = "";
            }
        }

        private void txtTrieuChung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                cmbLoaiBenh.Focus();
            }
        }

        private void cmbLoaiBenh_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            dtpNgayKham.Focus();
        }

        //Event DataGrid 

        private void TextBox_PreviewKeyDown_1(object sender, KeyEventArgs e)
        {
            if (!Char.IsDigit((char)KeyInterop.VirtualKeyFromKey(e.Key)) &&
               !Char.IsControl((char)KeyInterop.VirtualKeyFromKey(e.Key)))
            {
                e.Handled = true;
            }
        }

        private void cbThuoc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mathuoc = ((sender as ComboBox).SelectedItem as CThuoc).MaThuoc;
            dongiaban = ((sender as ComboBox).SelectedItem as CThuoc).DonGiaQuyDinh;
            SuaChiTiet(mathuoc, 0, 0, 0, viewPhieuKham.SelectedIndex);
        }

        private void cbDonVi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            madonvi = ((sender as ComboBox).SelectedItem as CDonVi).MaDonVi;
            SuaChiTiet(0, madonvi, 0, 0, viewPhieuKham.SelectedIndex);
        }

        private void cbCachDung_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            macachdung = ((sender as ComboBox).SelectedItem as CCachDung).MaCachDung;
            SuaChiTiet(0, 0, macachdung, 0, viewPhieuKham.SelectedIndex);
        }

        private void txtSoLuong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    soluong = int.Parse((sender as TextBox).Text);
                    SuaChiTiet(0, 0, 0, soluong, viewPhieuKham.SelectedIndex);
                }
                catch
                {
                }

                if (ChoPhepThemChiTiet())
                {
                    ThemChiTiet();
                    LuuChiTiet();
                }
                else if (viewPhieuKham.SelectedIndex == viewPhieuKham.Items.Count - 1)
                {
                    MessageBox.Show("Không Được Để Trống Ô Nhập Liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }

        }

        private void cbThuoc_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (viewPhieuKham.SelectedIndex == viewPhieuKham.Items.Count - 1 &&
                viewPhieuKham.SelectedIndex != -1 && (sender as ComboBox).SelectedIndex != -1 && KhongChoPhepSua())
            {
                e.Handled = true;
            }
        }

        private void cbDonVi_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (viewPhieuKham.SelectedIndex == viewPhieuKham.Items.Count - 1 &&
                viewPhieuKham.SelectedIndex != -1 && (sender as ComboBox).SelectedIndex != -1 && KhongChoPhepSua())
            {
                e.Handled = true;
            }
        }

        private void cbCachDung_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (viewPhieuKham.SelectedIndex == viewPhieuKham.Items.Count - 1 &&
                viewPhieuKham.SelectedIndex != -1 && (sender as ComboBox).SelectedIndex != -1 && KhongChoPhepSua())
            {
                e.Handled = true;
            }
        }

        private void txtSoLuong_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (viewPhieuKham.SelectedIndex == viewPhieuKham.Items.Count - 1 &&
               viewPhieuKham.SelectedIndex != -1 && (sender as TextBox).Text != "" && KhongChoPhepSua())
            {
                e.Handled = true;
            }
        }

        #endregion

        #region FUNCTION
        public void Refresh()
        {
            khambenh.LamMoi();
        }

        private void TaoBang()
        {
            thuoc = new CThuocContext(CConstant.CONNECTION);
            donvi = new CDonViContext(CConstant.CONNECTION);
            cachdung = new CCachDungContext(CConstant.CONNECTION);
            benh = new CBenhContext(CConstant.CONNECTION);
            khambenh = new CKhamBenhContext(CConstant.CONNECTION);
            benhnhan = new CBenhNhanContext(CConstant.CONNECTION);
            chitietkhambenh = new CChiTietKhamBenhContext(CConstant.CONNECTION);
            thamso = new CThamSoContext(CConstant.CONNECTION);

            cmbLoaiBenh.ItemsSource = benh.LayDanhSachTatCa();
            cmbLoaiBenh.DisplayMemberPath = "TenBenh";
            cmbLoaiBenh.SelectedValue = "MaLoaiBenh";
            makhambenh = khambenh.LayMaSoMoi();
            tienkham = thamso.LayThongTinThamSo().TienKham;
        }

        private void LamMoi()
        {
            //Clear Textbox
            txtMaBenhNhan.Clear();
            txtTenBenhNhan.Clear();
            txtTrieuChung.Clear();
            cmbLoaiBenh.SelectedIndex = -1;
            dtpNgayKham.Text = DateTime.Now.ToShortDateString();
        
            //Enabled Textbox
            txtMaBenhNhan.IsEnabled = true;
            txtTrieuChung.IsEnabled = true;
            cmbLoaiBenh.IsEnabled = true;
            dtpNgayKham.IsEnabled = true;
            viewPhieuKham.IsEnabled = true;

            txtMaBenhNhan.Focus();
            txtMaKhamBenh.Text = khambenh.LayMaSoMoi().ToString();
            viewPhieuKham.Items.Clear();
            ThemChiTiet();
            tienthuoc = 0;
        }

        private void LapPhieu()
        {
            try
            {
                if (ChoPhepLapPhieu())
                {
                    khambenh.Them(new CKhamBenh(
                        int.Parse(txtMaBenhNhan.Text),
                        dtpNgayKham.SelectedDate.Value,
                        txtTrieuChung.Text,
                        (cmbLoaiBenh.SelectedItem as CBenh).MaLoaiBenh,
                        tienkham,
                        tienthuoc));

                    for (int i = 0; i < listChiTiet.Count; i++)
                    {
                        chitietkhambenh.Them(listChiTiet[i]);
                    }
                    listChiTiet.Clear();
                    MessageBox.Show("Lập Phiếu Thành Công", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    txtMaBenhNhan.Focus();
                    MessageBox.Show("Không Được Để Trống Các Ô Nhập Liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch
            {
                MessageBox.Show("Lập Phiếu Thất Bại", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Error);
            } 
        }

        private void ThemChiTiet()
        {
            intSTT = viewPhieuKham.Items.Count + 1;
            viewPhieuKham.Items.Add(new
            {
                STT = intSTT.ToString(),
                TenThuoc = thuoc.LayDanhSachTatCa(),
                DonVi = donvi.LayDanhSachTatCa(),
                CachDung = cachdung.LayDanhSachTatCa()
            });

            mathuoc = 0;
            madonvi = 0;
            macachdung = 0;
            soluong = 0;
            
        }

        private void LuuChiTiet()
        {
            tienthuoc += soluong * dongiaban;
            listChiTiet.Add(new CChiTietKhamBenh(
                makhambenh,
                mathuoc,
                madonvi,
                macachdung,
                soluong,
                dongiaban));
        }

        private void SuaChiTiet(int thuoc, int donvi, int cachdung, int soluong,int indexView)
        {
            if (indexView < listChiTiet.Count - 1)
            {
                if (thuoc != 0)
                {
                    listChiTiet.ElementAt<CChiTietKhamBenh>(indexView).MaThuoc = thuoc;
                }

                if (donvi != 0)
                {
                    listChiTiet.ElementAt<CChiTietKhamBenh>(indexView).MaDonVi = donvi;
                }

                if (cachdung != 0)
                {
                    listChiTiet.ElementAt<CChiTietKhamBenh>(indexView).MaCachDung = cachdung;
                }

                if (soluong != 0)
                {
                    listChiTiet.ElementAt<CChiTietKhamBenh>(indexView).SoLuong = soluong;
                }
            }
        }

        private bool ChoPhepThemChiTiet()
        {
            if (mathuoc != 0 && madonvi != 0 && macachdung != 0 && soluong != 0)
            {
                return true;
            }
            return false;
        }

        private bool KhongChoPhepSua()
        {
            if (mathuoc == 0 && madonvi == 0 && macachdung == 0 && soluong == 0)
            {
                return false;
            }
            return true;
        }

        private bool ChoPhepLapPhieu()
        {
            if (txtMaBenhNhan.Text == "" || txtTrieuChung.Text == "" || cmbLoaiBenh.SelectedIndex == -1)
            {
                return false;
            }
            return true;
        }

        #endregion

      

        

    

    

















    }
}
